//index.js
//获取应用实例
const app = getApp()
Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  bindViewTap: function() {},
  onLoad: function() {

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
      gotoNext();
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
        gotoNext();
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
          gotoNext()
        }
      })
    }
    gotoNext()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onShow: function() {
    if (app.globalData.request_common_para != null && app.globalData.request_common_para.userInfoId) {
      if (app.globalData.request_common_para.openid) {
        wx.request({
          url: app.getHost()+"/datalake/v1/userInfo?openId=" + app.globalData.request_common_para.openid,
          method: "GET",
          success: res => {
            console.info("userinfo get success:" + JSON.stringify(res))
            if (res.statusCode == 200 && res.data && res.data.length > 0) {
              app.globalData.request_common_para.userInfoId = res.data[0].userInfoId;
              app.globalData.request_common_para.customer_name = res.data[0].customer_name;
              app.globalData.request_common_para.nickName = res.data[0].nick_name;
              app.globalData.request_common_para.roleName = res.data[0].type
              app.globalData.request_common_para.store_name = res.data[0].store_name
              let menuList =[]
              if (res.data[0].menu){
                menuList = res.data[0].menu.split(",");
              } 
              
              let menuListData = [];
              for (let i in menuList) {
                if (menuList[i] == "客户需求") {
                  let data = {
                    "id": i,
                    "name": "客户需求",
                    "url": "/page/cust/custInfoList"
                  }
                  menuListData.push(data)
                }
                if (menuList[i] == "需求审核") {
                  let data = {
                    "id": i,
                    "name": "需求审核",
                    "url": "/page/cust/custInfoAuditing"

                  }
                  menuListData.push(data)
                }
                if (menuList[i] == "需求汇总") {
                  let data = {
                    "id": i,
                    "name": "需求汇总",
                    "url": "/page/cust/custInfoStatistics"
                  }
                  menuListData.push(data)
                }
                if (menuList[i] == "成交战败") {
                  let data = {
                    "id": i,
                    "name": "成交战败",
                    "url": "/page/cust/custListFinish"
                  }
                  menuListData.push(data)
                }
              }
              if (app.globalData.menuListData.length != menuListData.length){
                    app.globalData.menuListData = menuListData
                    gotoNext();
                }
            }
          },
          fail: function(res) {
            console.info(" userinfo get fail:" + res)
          }
        })
      }
    }
  },
  getUserInfo: function(e) {
    let userInfo = e.detail.userInfo;
    app.globalData.userInfo = userInfo
    app.globalData.request_common_para.nickName = userInfo.nickName
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
    if (app.globalData.userInfo && !app.globalData.request_common_para.userInfoId) {
      // 登录
      wx.login({
        success: res => {
          console.info("page index wx.login:  " + JSON.stringify(res))
          wx.request({
            url: app.getHost()+'/datalake/v1/userManages/codeNew/' + res.code,
            success: res => {
              if (res.statusCode == 200) {
                console.info(app.getHost()+"/datalake/v1/userManages/codeNew/ result value:" + JSON.stringify(res));

                app.globalData.request_common_para.openid = res.data.openid
                /**根据openid查詢user對象 */
                console.info("this.globalData.request_common_para:" + JSON.stringify(app.globalData.request_common_para))
                if (app.globalData.request_common_para.openid) {
                  wx.request({
                    url: app.getHost()+"/datalake/v1/userInfo?openId=" + app.globalData.request_common_para.openid,
                    method: "GET",
                    success: res => {
                      console.info("userinfo get success:" + JSON.stringify(res))
                      if (res.statusCode == 200 && res.data && res.data.length > 0) {
                        app.globalData.request_common_para.userInfoId = res.data[0].userInfoId;
                        app.globalData.request_common_para.customer_name = res.data[0].customer_name;
                        app.globalData.request_common_para.nickName = res.data[0].nick_name;
                        app.globalData.request_common_para.roleName = res.data[0].type
                        app.globalData.request_common_para.store_name = res.data[0].store_name
                        let menuList = []
                        if (res.data[0].menu){
                          menuList = res.data[0].menu.split(",");
                        }
                        app.globalData.menuListData = [];
                        let menuListData = [];
                        for (let i in menuList) {
                          if (menuList[i] == "客户需求") {
                            let data = {
                              "id": i,
                              "name": "客户需求",
                              "url": "/page/cust/custInfoList"
                            }
                            menuListData.push(data)
                          }
                          if (menuList[i] == "需求审核") {
                            let data = {
                              "id": i,
                              "name": "需求审核",
                              "url": "/page/cust/custInfoAuditing"

                            }
                            menuListData.push(data)
                          }
                          if (menuList[i] == "需求汇总") {
                            let data = {
                              "id": i,
                              "name": "需求汇总",
                              "url": "/page/cust/custInfoStatistics"
                            }
                            menuListData.push(data)
                          }
                          if (menuList[i] == "成交战败") {
                            let data = {
                              "id": i,
                              "name": "成交战败",
                              "url": "/page/cust/custListFinish"
                            }
                            menuListData.push(data)
                          }
                        }
                        app.globalData.menuListData = menuListData
                        gotoNext();

                        /**
                        if (app.globalData.request_common_para.roleName == "经理") {
                          wx.reLaunch({
                            url: '/page/cust/custInfoAuditing',
                          })
                        }
						*/
                      } else if (res.statusCode == 500) {
                        let data = {};
                        data.nickName = app.globalData.request_common_para.nickName
                        data.openId = app.globalData.request_common_para.openid
                        wx.request({
                          url: app.getHost()+'/datalake/v1/userInfo ',
                          data: data,
                          method: "POST",
                          header: {
                            'content-type': 'application/json' // 默认值
                          },
                          success: res => {
                            console.info("create userInfo succes:" + JSON.stringify(res))
                            app.globalData.request_common_para.userInfoId = res.data.userInfoId;
                            app.globalData.request_common_para.customer_name = res.data.customer_name;
                            app.globalData.request_common_para.nickName = res.data.nick_name;
                            app.globalData.request_common_para.roleName = res.data.type
                            let menuList = res.data.menu.split(",");
                            app.globalData.menuListData = [];
                            let menuListData = [];
                            for (let i in menuList) {
                              if (menuList[i] == "客户需求") {
                                let data = {
                                  "id": i,
                                  "name": "客户需求",
                                  "url": "/page/cust/custInfoList"
                                }
                                menuListData.push(data)
                              }
                              if (menuList[i] == "需求审核") {
                                let data = {
                                  "id": i,
                                  "name": "需求审核",
                                  "url": "/page/cust/custInfoAuditing"

                                }
                                menuListData.push(data)
                              }
                              if (menuList[i] == "需求汇总") {
                                let data = {
                                  "id": i,
                                  "name": "需求汇总",
                                  "url": "/page/cust/custInfoStatistics"
                                }
                                menuListData.push(data)
                              }
                              if (menuList[i] == "成交战败") {
                                let data = {
                                  "id": i,
                                  "name": "成交战败",
                                  "url": "/page/cust/custListFinish"
                                }
                                menuListData.push(data)
                              }
                            }

                            gotoNext();

                            /**
                            if (app.globalData.request_common_para.roleName == "经理") {
                              wx.reLaunch({
                                url: '/page/cust/custInfoAuditing',
                              })
                            }
							*/
                          },
                          fail: function(res) {
                            console.error("create userInfo err:" + JSON.stringify(res));
                          }
                        })
                      }
                    },
                    fail: function(res) {
                      console.info(" userinfo get fail:" + res)
                    }

                  })
                }
              }
            },
            fail: res => {}
          })
        }
      })



      /**
      wx.request({
        url: 'https://mcc.mtmche.cn/datalake/v1/userInfo',
        data: data,
        method: "POST",
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function(res) {
          console.info("page index create userInfo succes:" + JSON.stringify(res))
          if (res.statusCode = 200) {
            app.globalData.request_common_para.userInfoId = res.data[0].userInfoId;
            app.globalData.request_common_para.customer_name = res.data[0].customer_name;
            app.globalData.request_common_para.nickName = res.data[0].nick_name;
            app.globalData.request_common_para.roleName = res.data[0].type
            let menuList = res.data[0].menu.split(",");
            app.globalData.menuListData = [];
            let menuListData = [];
            for (let i in menuList) {
              if (menuList[i] == "客户需求") {
                let data = {
                  "id": i,
                  "name": "客户需求",
                  "url": "/page/cust/custInfoList"
                }
                menuListData.push(data)
              }
              if (menuList[i] == "需求审核") {
                let data = {
                  "id": i,
                  "name": "需求审核",
                  "url": "/page/cust/custInfoAuditing"

                }
                menuListData.push(data)
              }
              if (menuList[i] == "需求汇总") {
                let data = {
                  "id": i,
                  "name": "需求汇总",
                  "url": "/page/cust/custInfoStatistics"
                }
                menuListData.push(data)
              }
            }
            app.globalData.menuListData = menuListData
            if (app.globalData.request_common_para.roleName == "销售") {
              wx.reLaunch({
                url: '/page/cust/custInfoList',
              })
            }
            if (app.globalData.request_common_para.roleName == "经理") {
              wx.reLaunch({
                url: '/page/cust/custInfoAuditing',
              })
            }
          }
        },
        fail: function(res) {
          console.error("page index create userInfo err:" + JSON.stringify(res));
        }
      })


  */

    }
  }
})

function gotoNext() {
  if (app.globalData.request_common_para.userInfoId) {
    if (app.globalData.menuListData != null && app.globalData.menuListData.length > 0) {
      wx.reLaunch({
        url: app.globalData.menuListData[0].url,
      });
    } else {
      app.noTitlemodalTap("无权限进入系统，请联系管理员")
    }
  }
}